package com.zzq.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zzq.constant.MessageConstant;
import com.zzq.entity.Result;
import com.zzq.service.ReportService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 幼儿猿大班
 * 2021/7/30
 */
@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private ReportService reportService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){

        try {
            Map<String,Object> map=reportService.findMonthAndMemberCount();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){

        try {
            Map<String,Object> map= reportService.findSetmealNameAndCount();
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){

        try {
            Map<String,Object> map= reportService.getBusiness();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }

    @RequestMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String,Object> map= reportService.getBusiness();

        String path=request.getSession().getServletContext().getRealPath("template"+File.separator+"report_template.xlsx");

        Workbook workbook=new XSSFWorkbook(new FileInputStream(new File(path)));

        Sheet sheetAt = workbook.getSheetAt(0);

        sheetAt.getRow(2).getCell(5).setCellValue(map.get("reportDate").toString());
        sheetAt.getRow(4).getCell(5).setCellValue(Integer.valueOf(map.get("todayNewMember").toString()));
        sheetAt.getRow(4).getCell(7).setCellValue(Integer.valueOf(map.get("totalMember").toString()));
        sheetAt.getRow(5).getCell(5).setCellValue(Integer.valueOf(map.get("thisWeekNewMember").toString()));
        sheetAt.getRow(5).getCell(7).setCellValue(Integer.valueOf(map.get("thisMonthNewMember").toString()));
        sheetAt.getRow(7).getCell(5).setCellValue(Integer.valueOf(map.get("todayOrderNumber").toString()));
        sheetAt.getRow(7).getCell(7).setCellValue(Integer.valueOf(map.get("todayVisitsNumber").toString()));
        sheetAt.getRow(8).getCell(5).setCellValue(Integer.valueOf(map.get("thisWeekOrderNumber").toString()));
        sheetAt.getRow(8).getCell(7).setCellValue(Integer.valueOf(map.get("thisWeekVisitsNumber").toString()));
        sheetAt.getRow(9).getCell(5).setCellValue(Integer.valueOf(map.get("thisMonthOrderNumber").toString()));
        sheetAt.getRow(9).getCell(7).setCellValue(Integer.valueOf(map.get("thisMonthVisitsNumber").toString()));

        List<Map<String,Object>> hotSetmeal = (List) map.get("hotSetmeal");
        int i=0;
        for ( Map o: hotSetmeal) {
            sheetAt.getRow(12+i).getCell(4).setCellValue(o.get("name").toString());
            sheetAt.getRow(12+i).getCell(5).setCellValue(Integer.valueOf(o.get("setmeal_count").toString()));
            sheetAt.getRow(12+i).getCell(6).setCellValue(Double.parseDouble(o.get("proportion").toString()));
            i++;
        }

        ServletOutputStream out = response.getOutputStream();
        // 下载的数据类型（excel类型）
        response.setContentType("application/vnd.ms-excel");
        // 设置下载形式(通过附件的形式下载)
        response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
        workbook.write(out);

        out.flush();
        out.close();
        workbook.close();

    }
}
